//  ==========
//  = This file stores all of the scripts that would normally be placed inside the layout.master
//  = Use the following syntax for referencing this script from the layout.master.
//  = <portal:SkinFolderScript ID="sfs1" runat="server" ScriptFileName="script.js" AddToCombinedScript="true" />
//  ==========


// Content edit link toggle array
var linksToHide = '.forumEdit, .modulelinks, .ModuleEditLink, .postEdit, .galleryedit, .feedlist .editlink, #editLink, .threadEdit, .forumpostusername a[id*="Hyperlink2"], .linksmodule [id*="editLink2"]';

// Content Edit Links
function HideEditLinks() {
	$(linksToHide).hide();
	Set_Cookie('editLinksState', 'hidden');
}

function ShowEditLinks() {
	$(linksToHide).show();
	Set_Cookie('editLinksState', 'visible');
}

// jQuery Scripts
$(document).ready(function() {


	// content edit links
	var editLinksState = Get_Cookie('editLinksState');
	if (editLinksState === null || editLinksState == 'visible') {
		ShowEditLinks();
	} else if (editLinksState == 'hidden') {
		HideEditLinks();
	}

	$("a#togglemodulelinks").click(function() {
		editLinksState = Get_Cookie('editLinksState');
		if (editLinksState === null || editLinksState == 'visible') {
			HideEditLinks();
		} else if (editLinksState == 'hidden') {
			ShowEditLinks();
		}
		return false;
	});


	// Superfish Menu
	// Get Superfish mojoPortal Script
	$.getScript("/ClientScript/jqmojo/mojosuperfish150.js", function() {
		$("ul.sf-menu").supersubs({
			minWidth: 10,
			maxWidth: 35,
			extraWidth: 2
		}).superfish({
			pathClass: 'current',
			pathLevels: 0,
			//animation: {height:'show'},
			delay: 800,
			speed: 'normal',
			dropShadows: false,
			autoArrows: false
		});
	});


	// This will make the Shared Files "Add File" button have a hover state
	$("div.uploadpanel > div > input + div + div").hover(

		function() {
			$("div.uploadpanel div .jqbutton").addClass("ui-state-hover");
		},
		function() {
			$("div.uploadpanel div .jqbutton").removeClass("ui-state-hover");
		});


	// Seperate first word from second - site title
	$('.siteheading').each(function() {
		$(this).logofirstWord();
		$(this).logosecondWord();
	});

	// Scroll-along nav on blog
	if ($(".blogwrapper .blogtext").length !== 0 || $(".blogmodule .blogtext").length !== 0 || $(".rssfeedmodule").length !== 0) {
		var $blognav = $(".blognavright, .blognavleft, .rssnavright, .rssnavleft"),
			$blogcontent = $(".blogcenter-rightnav, .blogcenter-leftnav, .rsscenter-rightnav, .rsscenter-leftnav").outerHeight(true),
			offset = $blognav.offset(),
			paddingtop = 20,
			navheight = $blognav.outerHeight(true),
			maxmargin = $blogcontent - navheight,
			lastscrollpos = $(window).scrollTop();

		// If logged in, change the distance from the top to compensate for the presence of the admin bar    
		if ($("body.pagebody").hasClass("loggedin")) {
			var paddingtop = 60;
		}

		// If content is smaller than nav, remove content height from max margin
		if ($blogcontent < navheight) {
			var maxmargin = navheight;
		}

		$(window).scroll(function() {
			var maxnavheight = $(window).height() - paddingtop; // disables scroller if the nav gets taller than the user screen
			// check to see if you've scrolled far enough to not show the nav, and not far enough to slide into the footer
			if ($(window).scrollTop() > offset.top && $(window).scrollTop() < maxmargin && navheight < maxnavheight) {
				$blognav.stop().animate({
					marginTop: $(window).scrollTop() - offset.top + paddingtop
				}, {
					queue: false,
					duration: 600,
					easing: 'easeOutExpo'
				});
			}
			// if not, check if you've scrolled into the footer
			else if ($(window).scrollTop() > maxmargin && navheight < maxnavheight) {
				// snap to bottom
				$blognav.stop().animate({
					marginTop: maxmargin
				}, {
					queue: false,
					duration: 600,
					easing: 'easeOutExpo'
				});
			} else {
				// else snap to top
				$blognav.stop().animate({
					marginTop: 0
				}, {
					queue: false,
					duration: 600,
					easing: 'easeOutExpo'
				});
			}
		});
	}

	// back to top button
	$(window).scroll(function() {
		if ($(window).scrollTop() > 65 && $(window).width() > 1330) {
			$(".backtotop").fadeIn();
		} else if ($(window).scrollTop() < 65 || $(window).width() < 1330) {
			$(".backtotop").fadeOut();
		}
	});

	$(".backtotop").click(function() {
		$('body,html').animate({
			scrollTop: 0
		}, 800);
		return false;
	});

	$(window).resize(function() {
		if ($(window).width() < 1330) {
			$(".backtotop").fadeOut();
		} else {
			$(".backtotop").fadeIn();
		}
	});

	// Event Cal Pro fix
	$(".eventmonthview").addClass("js-active"); // Add js-active class for styling

	// Stupid IE doesn't like spaces in the selector - don't delete the second one even though it looks like a dupe
	$(".eventmonthview .mpcalendarmain tbody tr td[style=\"width:14%;border-top-width: 0px;\"], .eventmonthview .mpcalendarmain tbody tr td[style=\"width: 14%; border-top-width: 0px;\"]").each(function() {
		$(this).parent().addClass("beforelast");
	});

	$(".beforelast").after('<tr class="lastrow"><td></td></tr>');
	$(".dayNumber").parent().parent().addClass("firstrow");

});

$.fn.logofirstWord = function() {
	var node = this.contents().filter(function() {
			return this.nodeType == 3;
		}).first(),
		text = node.text(),
		firstword = text.split(" ", 1).join(" ");

	if (!node.length) {
		return;
	}
	node[0].nodeValue = text.slice(firstword.length);
	node.before('<span class="firstword">' + firstword + '</span>');
};
$.fn.logosecondWord = function() {
	var node = this.contents().filter(function() {
			return this.nodeType == 3;
		}).first(),
		text = node.text(),
		secondword = text.split(" ", 2).join(" ");

	if (!node.length) {
		return;
	}
	node[0].nodeValue = text.slice(secondword.length);
	node.before('<span class="secondword">' + secondword + '</span>');
};